home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cpost_1_4.lha / cpostp1.c < prev    next >
C/C++ Source or Header  |  1997-11-17  |  18KB  |  579 lines

  1. /*------------------------------------------------------------------
  2.  * cpostp1.c : Pass 1 of cPost
  3.  *------------------------------------------------------------------
  4.  * 12-02-91 originally by Patrick J. Mueller
  5.  * 12-03-92 converted from cBook to cPost
  6.  *------------------------------------------------------------------*/
  7.  
  8. #ifdef AMIGA
  9. #define USE_BUILTIN_MATH
  10. #endif /* AMIGA */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #include "ctok.h"
  17. #include "cpost.h"
  18.  
  19. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  20. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  21.  
  22. /*------------------------------------------------------------------
  23.  * add bracketing starts in front of braces
  24.  *------------------------------------------------------------------*/
  25. static void AddLBracket(
  26.    File *file,
  27.    char *line,
  28.    char *mask,
  29.    int  *maxMask,
  30.    Tok  *tok
  31.    )
  32.    {
  33.    int  lIndent;
  34.    int  rIndent;
  35.    int  place;
  36.    Tok *sib;
  37.  
  38.    /*---------------------------------------------------------------
  39.     * see if matching bracket is on this line - if so, skip
  40.     *---------------------------------------------------------------*/
  41.    sib = tok->sib;
  42.    if (sib && (sib->tok.line == tok->tok.line))
  43.       return;
  44.  
  45.    /*---------------------------------------------------------------
  46.     * find minimum indent of left and right brace
  47.     *---------------------------------------------------------------*/
  48.    lIndent = strspn(line," ");
  49.    if (!tok->sib)
  50.       place = lIndent;
  51.    else
  52.       {
  53.       rIndent = strspn(file->line[sib->tok.line-1]," ");
  54.       place = min(lIndent,rIndent);
  55.       }
  56.  
  57.    /*---------------------------------------------------------------
  58.     * if no indentation on { or }, don't bracket!
  59.     *---------------------------------------------------------------*/
  60.    if (!place)
  61.       return;
  62.  
  63.    /*---------------------------------------------------------------
  64.     * we'll bracket the column BEFORE this
  65.     *---------------------------------------------------------------*/
  66.    place--;
  67.  
  68.    /*---------------------------------------------------------------
  69.     * set the mask, maxMask, and write bracket to line
  70.     *---------------------------------------------------------------*/
  71.    mask[place]++;
  72.    *maxMask = max(place,*maxMask);
  73.  
  74.    if (' ' == line[place])
  75.       line[place] = '\x01';
  76.    }
  77.  
  78. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  79. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  80.  
  81. /*------------------------------------------------------------------
  82.  * add bracketing ends
  83.  *------------------------------------------------------------------*/
  84. static void AddRBracket(
  85.    File *file,
  86.    char *line,
  87.    char *mask,
  88.    int  *maxMask,
  89.    Tok  *tok
  90.    )
  91.    {
  92.    Tok *sib;
  93.  
  94.    /*---------------------------------------------------------------
  95.     * see if matching bracket is on this line - if so, skip
  96.     *---------------------------------------------------------------*/
  97.    sib = tok->sib;
  98.    if (sib && (sib->tok.line == tok->tok.line))
  99.       return;
  100.  
  101.    /*---------------------------------------------------------------
  102.     * safety valve
  103.     *---------------------------------------------------------------*/
  104.    if (-1 == *maxMask)
  105.       return;
  106.  
  107.    /*---------------------------------------------------------
  108.     * add end bracket to line
  109.     *---------------------------------------------------------*/
  110.    if (' ' == line[*maxMask])
  111.       line[*maxMask] = '\x03';
  112.  
  113.    /*---------------------------------------------------------
  114.     * reset mask and maxMask
  115.     *---------------------------------------------------------*/
  116.    mask[*maxMask]--;
  117.    if (!mask[*maxMask])
  118.       {
  119.       *maxMask -= 1;
  120.       while (-1 != *maxMask)
  121.          if (mask[*maxMask])
  122.             break;
  123.          else
  124.             *maxMask -= 1;
  125.       }
  126.    }
  127.  
  128.  
  129. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  130. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  131.  
  132. /*------------------------------------------------------------------
  133.  * add bracketing chars to line based on mask
  134.  *------------------------------------------------------------------*/
  135. static char *AddMBrackets(
  136.    File *file,
  137.    char *line,
  138.    char *mask,
  139.    int   maxMask
  140.    )
  141.    {
  142.    int   i;
  143.  
  144.    /*------------------------------------------------------------
  145.     * see if we need to make the line longer
  146.     *------------------------------------------------------------*/
  147.    if (maxMask + 1 >= (int) strlen(line))
  148.       {
  149.       char *newLine;
  150.  
  151.       newLine = malloc(maxMask+3);
  152.       if (!newLine)
  153.          cPostError(1,"out of memory!!!");
  154.  
  155.       if ('\n' == line[strlen(line)-1])
  156.          line[strlen(line)-1] = ' ';
  157.  
  158.       memset(newLine,' ',maxMask+1);
  159.       newLine[maxMask+1] = '\n';
  160.       newLine[maxMask+2] = 0;
  161.       memcpy(newLine,line,strlen(line));
  162.  
  163.       free(line);
  164.       line = newLine;
  165.       }
  166.  
  167.    /*---------------------------------------------------------------
  168.     * get number of first non-blank column
  169.     *---------------------------------------------------------------*/
  170.    maxMask = min(maxMask,(int)strspn(line," "));
  171.  
  172.    /*---------------------------------------------------------------
  173.     * for each non-zero entry in mask, write bracket
  174.     *---------------------------------------------------------------*/
  175.    for (i=0; i<=maxMask; i++)
  176.       {
  177.       if (mask[i])
  178.          if (' ' == line[i])
  179.             line[i] = '\02';
  180.       }
  181.  
  182.    return line;
  183.    }
  184.  
  185. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  186. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  187.  
  188. /*------------------------------------------------------------------
  189.  * strip trailing blanks off a line (that has a \n at the end!!)
  190.  *------------------------------------------------------------------*/
  191. static void StripTrailingBlanks(
  192.    char *line
  193.    )
  194.    {
  195.    int slen;
  196.  
  197.    slen = strlen(line);
  198.    if ('\n' != line[slen-1])
  199.       {
  200.       fprintf(stderr,"line found without carriage return!!\n");
  201.       return;
  202.       }
  203.  
  204.    line[slen-1] = ' ';
  205.  
  206.    while (*line && (' ' == line[slen-1]))
  207.       {
  208.       line[slen-1] = '\0';
  209.       slen--;
  210.       }
  211.  
  212.    line[slen] = '\n';
  213.    }
  214.  
  215. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  216. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  217.  
  218. /*------------------------------------------------------------------
  219.  * add function information to trees and lists
  220.  *------------------------------------------------------------------*/
  221. static void AddFunctionPrototype(
  222.    Info          *info,
  223.    File          *file,
  224.    char          *name
  225.    )
  226.    {
  227.    Function *func;
  228.  
  229.    func = GetFunction(info,name);
  230.  
  231.    if (!ListFind(file->funcProList,&func))
  232.       if (!ListAdd(file->funcProList,&func))
  233.          cPostError(1,"error adding function prototype to list");
  234.    }
  235.  
  236. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  237. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  238.  
  239. /*------------------------------------------------------------------
  240.  * add function information to trees and lists
  241.  *------------------------------------------------------------------*/
  242. static void AddFunctionDefinition(
  243.    Info          *info,
  244.    File          *file,
  245.    char          *name,
  246.    unsigned long  lineNo
  247.    )
  248.    {
  249.    Function *func;
  250.  
  251.    func = GetFunction(info,name);
  252.    if (!ListFind(file->funcDefList,&func))
  253.       if (!ListAdd(file->funcDefList,&func))
  254.          cPostError(1,"error adding function definition to list");
  255.  
  256.    func->fileName = file->name;
  257.    func->lineNo   = lineNo;
  258.    }
  259.  
  260. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  261. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  262.  
  263. /*------------------------------------------------------------------
  264.  * add function information to trees and lists
  265.  *------------------------------------------------------------------*/
  266. static void AddFunctionUsage(
  267.    Info          *info,
  268.    char          *calleeName,
  269.    char          *callerName
  270.    )
  271.    {
  272.    Function *caller;
  273.    Function *callee;
  274.  
  275.    callee = GetFunction(info,calleeName);
  276.    caller = GetFunction(info